// pages/phoneLogin/phoneLogin.js
const common = require('../../libs/common');
const HTTP = require("../../utils/util");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:1,
        phone: '',
        varify_ode: '',
        pass_word: '',
        invitation_code: '',
        is_read:false
    },
    postPhoneLogin(){
        HTTP.postData('/api/auth/login',{
            mobile:this.data.phone,
            password:this.data.pass_word,
            openid:wx.getStorageSync('openid')
        },res=>{
            console.log(res);
            if(res.code === 1020){
                wx.setStorageSync('token',res.data.token);
                wx.switchTab({
                    url:'../index/index'
                })
            }else{
                wx.showToast({
                    icon:'none',
                    title:res.msg
                })
            }
        })
    },
    phoneLogin(){
        if(!common.checkPhone(this.data.phone)){
            return false;
        }else if(!this.data.pass_word){
            wx.showToast({
                icon:'none',
                title:'请输入密码',
                duration:500
            });
            return false;
        }else {
            this.postPhoneLogin();
        }
    },
    getPhoneNumber(e){
        this.setData({
            phone:e.detail.value
        })
    },
    getPassWord(e){
        this.setData({
            pass_word:e.detail.value
        })
    },
    handleRead(){
        this.setData({
            is_read:!this.data.is_read
        })
    },
    formSubmit(e) {
        console.log(e.detail.value);
        let value = e.detail.value;
        if (!common.checkPhone(value.phone)) {
            return false;
        } else if (!common.checkVerifyCode(value.verify_code)) {
            return false;
        } else if (!value.pass_word) {
            wx.showToast({
                icon: 'none',
                title: '请输入密码'
            });
            return false;
        } else if (!value.pass_word2) {
            wx.showToast({
                icon: 'none',
                title: '请再次输入密码'
            });
            return false;
        }else if(!this.data.is_read) {
            wx.showToast({
                icon: 'none',
                title: '请阅读《用户服务协议》，《隐私政策》'
            });
            return false;
        } else {
            this.setData({
                phone: value.phone,
                verify_code: value.verify_code,
                pass_word: value.pass_word,
                pass_word2: value.pass_word2,
                invitation_code: value.invitation_code,
            });
            console.log('可以发送请求');
            this.postUserInfo();
        }
    },
    postUserInfo() {
        let data = {
            openid:wx.getStorageSync('openid'),
            password: this.data.pass_word,
            password_confirm: this.data.pass_word2,
            invitation_code: this.data.invitation_code,
            mobile: this.data.phone,
            verify_code: this.data.verify_code,
            is_agree:1
        };
        HTTP.postData('/api/auth/register', data, res => {
            if (res.code === 1020) {
                wx.setStorageSync('token', res.data.token);
                wx.switchTab({
                    url: '../index/index'
                });
            }
        });
    },
    phoneBlur(e) {
        console.log(e)
        this.setData({
            phone: e.detail.value
        });
    },    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options['invitation_code']){
            this.setData({
                id:1,
                invitation_code:options['invitation_code']
            });
        }else{
            this.setData({
                id:+options.id,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})